//
//  PageViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/30.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class PageViewController: KMViewController {
    
    fileprivate let cellId = "pageCell"
    
    var collectionView : UICollectionView!
    
    //控制器数
    var viewControlers : [UIViewController]! {
        
        didSet{
            self.createCollectionView()
        }
    }
    
    //当前控制器
    var currentVC:Int!
    
    //标题
    var titles : [String]!
    
    //MARK: 创建上边标题
    fileprivate func createCollectionView() {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets.zero
        layout.itemSize = CGSize.init(width: ScreenW/CGFloat(viewControlers.count), height: StatusBarAddNavBarHeight - StatusBarHeight)
        
        collectionView = UICollectionView.init(frame: .init(x: 0, y: 0, width: ScreenW, height: StatusBarAddNavBarHeight - StatusBarHeight), collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(PageCell.self, forCellWithReuseIdentifier: cellId)
        self.collectionView(collectionView, didSelectItemAt: IndexPath.init(row: currentVC, section: 0))
        self.navigationController?.navigationBar.addSubview(collectionView)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension PageViewController : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return viewControlers.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? PageCell
        cell?.titleLabel.text = titles[indexPath.row]
        
        if indexPath.row == currentVC {
            cell?.titleLabel.textColor = UIColor.yellow
        }
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.row != currentVC {
            let firstCell = self.collectionView.cellForItem(at: IndexPath.init(row: currentVC, section: 0)) as? PageCell
            firstCell?.titleLabel.textColor = UIColor.white
        }
        
        let cell = collectionView.cellForItem(at: indexPath) as? PageCell
        cell?.titleLabel.textColor = UIColor.yellow
        
        let viewController = viewControlers[indexPath.row]
        
        //判断页面是否存在，不存在则添加，存在则显示
        if self.isCurrentViewControllerVisible(viewcontroller: viewController) {
            self.view.bringSubview(toFront: viewController.view)
        }else{
            self.addChildViewController(viewController)
            self.didMove(toParentViewController: self)
            viewController.view.frame = self.view.bounds
            self.view.addSubview(viewController.view)
        }
        
//        if let tabBarVC = self.view.window?.rootViewController as? RootTabBarController {
//            switch titles[indexPath.row] {
//            case loan_title:
//                tabBarVC.selectedIndex = 2
//            case creditCard:
//                tabBarVC.selectedIndex = 1
//            default:
//                break
//            }
//        }
        
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
        
        let cell = collectionView.cellForItem(at: indexPath) as? PageCell
        cell?.titleLabel.textColor = UIColor.white
    }
    
}
